/**
 * 本质上就是给定一个树，然后做N-1次操作，每次将一条路径上的点权全部加1，终点不加
 * 最后问每个点权是多少。初始点权全为零
 */
#include <bits/stdc++.h>
using namespace std;

struct HLD{ // 重链剖分

using llt = int;

using value_type = llt;
vector<value_type> data; // 线段树

using lazy_type = llt;
vector<lazy_type> lazy; // 延迟标记

/// 从下往上计算信息，要变动
value_type _up_(const value_type & ls, const value_type & rs) {
    return ls + rs;
}

/// 从上往下计算信息，要变动
void _dn_(int t, int s, int e, const lazy_type & delta) {
    // assert(0);
    int n = e - s + 1;
    data[t] += n * delta;
    lazy[t] += delta;
}

static value_type mkValue(llt data){
    // assert(0);
    return data;
}

/// 辅助函数，视延迟的类型而变动
static const lazy_type & lazy_zero() {
    static const lazy_type LAZY0 = 0;
    return LAZY0; 
}

/// 辅助函数，视线段树信息类型而变动
static const value_type & value_zero() {
    static const value_type VALUE0 = 0;
    return VALUE0;
}

/// 几乎不用动
value_type _query(int t, int s, int e, int a, int b) {
    if(a <= s and e <= b) {
        return data[t];
    }
    _pushDown(t, s, e);
    int mid = (s + e) >> 1;
    value_type ans = value_zero();
    if(a <= mid) ans = _up_(ans, _query(lson(t), s, mid, a, b));
    if(mid < b) ans = _up_(ans, _query(rson(t), mid + 1, e, a, b));
    return ans;
}

/// 几乎不用动
void _modify(int t, int s, int e, int a, int b, const lazy_type & delta) {
    if(a <= s and e <= b) {
        _dn_(t, s, e, delta);
        return;
    }
    _pushDown(t, s, e);
    int mid = (s + e) >> 1;
    if(a <= mid) _modify(lson(t), s, mid, a, b, delta);
    if(mid < b) _modify(rson(t), mid + 1, e, a, b, delta);
    _pushUp(t);
    return;
}

/// 这个函数不用动
void _pushUp(int t) {
    data[t] = _up_(data[lson(t)], data[rson(t)]);
}

/// 这个函数几乎不用动
void _pushDown(int t, int s, int e) {
    if(lazy_zero() == lazy[t]) return;
    auto & lz = lazy[t];
    auto ls = lson(t), rs = rson(t);
    int mid = (s + e) >> 1;

    _dn_(ls, s, mid, lz);
    _dn_(rs, mid + 1, e, lz);

    lz = lazy_zero();
}

/// 这两个函数不用变动
static int lson(int x) {return x << 1;}
static int rson(int x) {return lson(x) | 1;}

/// 树结构, 1-index
vector<vector<int>> g;
/// 点权值
vector<llt> weight;

/// 建单向边
void mkDiEdge(int a, int b){
    g[a].push_back(b);
}
/// 建双向边
void mkBiEdge(int a, int b){
    mkDiEdge(a, b); mkDiEdge(b, a);
}

/// 树链剖分结构
struct node_t{
    int parent; // 父节点
    int hson;   // 重儿子
    int depth;  // 该节点的深度, 根节点深度为0
    int size;   // 本节点所领子树的节点总数
    int top;    // 本节点所在重链的顶
    int nid;    // 本节点在线段树中的编号
    int mdes;   // 本节点所领子树的线段树编号均在[nid, mdes]中
};

int root; // 树根
vector<int> nid2old; // nid2old[i]表示线段树中第i个节点在原树中的编号
int timestamp; // 辅助变量
vector<node_t> nodes;

/// 递归找重边
void _dfsHeavyEdge(int u, int p, int d){
    auto & n = nodes[u];
    n.parent = p;
    n.depth = d;
    n.size = 1;

    for(auto v : g[u]){
        if(v == p) continue;
        _dfsHeavyEdge(v, u, d + 1);
        n.size += nodes[v].size;
        if(nodes[n.hson].size < nodes[v].size) n.hson = v;
    }
    return;
}

/// 递归找重链
void _dfsHeavyPath(int u, int top){
    auto & n = nodes[u];
    n.top = top;
    nid2old[n.mdes = n.nid = ++timestamp] = u;

    if(0 == n.hson) return;
    _dfsHeavyPath(n.hson, top);
    n.mdes = max(n.mdes, nodes[n.hson].mdes);

    for(auto v : g[u]){
        if(v != n.parent and v != n.hson){
            _dfsHeavyPath(v, v);
            n.mdes = max(n.mdes, nodes[v].mdes);
        }
    }
    return;
}
/// 递归建线段树
void _build(int t, int s, int e) {
    if(s == e) {
        data[t] = mkValue(weight[nid2old[s]]); // 注意线段树编号与原树编号存在转换
        return; 
    }
    int mid = (s + e) >> 1;
    _build(lson(t), s, mid);
    _build(rson(t), mid + 1, e);
    _pushUp(t);
}

/// 初始化, n是树的点数
void init(int n){
    timestamp = 0;
    /// 初始化树结构
    g.assign(n + 1, {});
    weight.assign(n + 1, 0);
    /// 初始化树链结构
    nodes.assign(n + 1, {0, 0, 0, 0, 0, 0, 0});
    nid2old.assign(n + 1, 0);
    /// 初始化线段树结构
    data.assign(n + 1 << 2, value_zero());
    lazy.assign(n + 1 << 2, lazy_zero());    
}

/// 在输入所有数据以后构建
void build(int root, int n){
    /// 建树链
    _dfsHeavyEdge(this->root = root, 0, 0);
    _dfsHeavyPath(root, root);
    /// 建线段树
    _build(1, 1, n);
}

/// 原树上x到y的路径修改
void modify(int x, int y, const lazy_type & delta){
    int n = g.size() - 1;
    while(nodes[x].top != nodes[y].top){
        if(nodes[nodes[x].top].depth < nodes[nodes[y].top].depth) swap(x, y);

        _modify(1, 1, n, nodes[nodes[x].top].nid, nodes[x].nid, delta);
        x = nodes[nodes[x].top].parent;
    }
    if(nodes[x].depth > nodes[y].depth) swap(x, y);
    _modify(1, 1, n, nodes[x].nid, nodes[y].nid, delta);
    return;
}

/// 查询原树上x到y的路径信息
value_type query(int x, int y){
    int n = g.size() - 1;
    value_type ans = value_zero();
    while(nodes[x].top != nodes[y].top){
        if(nodes[nodes[x].top].depth < nodes[nodes[y].top].depth) swap(x, y);

        ans = _up_(ans, _query(1, 1, n, nodes[nodes[x].top].nid, nodes[x].nid));
        x = nodes[nodes[x].top].parent;
    }
    if(nodes[x].depth > nodes[y].depth) swap(x, y);
    ans = _up_(ans, _query(1, 1, n, nodes[x].nid, nodes[y].nid));
    return ans;
}

}St;


int N;
vector<int> A;

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> N;
    A.assign(N, 0);
    for(auto & i : A) cin >> i;
    St.init(N);
    for(int a,b,i=1;i<N;++i){
        cin >> a >> b;
        St.mkBiEdge(a, b);
    } 
    St.build(1, N);
    for(int i=1;i<N;++i){
        St.modify(A[i - 1], A[i], 1);
        St.modify(A[i], A[i], -1);
    }
    for(int i=1;i<=N;++i){
        cout << St.query(i, i) << endl;
    }
    return 0;
}